package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.NovelEditHistoryAdapter;
import com.taptech.doufu.event.EventBusBackup;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.bean.NovelEditHistoryGsonBean;
import com.taptech.doufu.ui.view.WaitDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class NovelEditHistoryActivity extends DiaobaoBaseActivity {

    private WaitDialog dialog;

    private RecyclerView recyclerView;
    private TextView tvTip;
    private LinearLayout llHelp;
    public static final String INTENT_KEY_NOVEL_ID = "intent_key_novel_id";
    public static final String INTENT_KEY_CHAPTER_ID = "intent_key_chapter_id";
    private LinearLayout llNull;

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onResetBackup(EventBusBackup event) {
        if (event != null && event.getTag().equals(EventBusBackup.tag) && event.getModel()!=null) {
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_novel_edit_history);
        EventBus.getDefault().register(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        llHelp = (LinearLayout) findViewById(R.id.llHelp);
        llHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(NovelEditHistoryActivity.this, BrowseActivity.class);
                intent.putExtra("URL","http://api.doufu.la/journal/detail?id=522");
                startActivity(intent);
            }
        });
        tvTip = (TextView) findViewById(R.id.tvTip);
        llNull = (LinearLayout) findViewById(R.id.llNull);
        String novelId = getIntent().getStringExtra(INTENT_KEY_NOVEL_ID);
        String chapterId = getIntent().getStringExtra(INTENT_KEY_CHAPTER_ID);
        showDialog();
        getData(novelId,chapterId);
    }

    private void showDialog() {
        dialog = new WaitDialog(this, R.style.updateDialog);
        dialog.show();
    }

    private void dimsDialog() {
        if (!isFinishing() && dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    private void getData(String novelId,String chapterId) {
        ApiClient.getInstance().setUseCache(false).getService().getNovelEditHistory(novelId,chapterId)
                .compose(RxJavaHelper.<NovelEditHistoryGsonBean>observeOnMainThread(this))
                .subscribe(new BaseSubscriber<NovelEditHistoryGsonBean>(){
                    @Override
                    public void onNext(NovelEditHistoryGsonBean model) {
                        super.onNext(model);
                        if (model != null && model.getData() != null) {
                            NovelEditHistoryGsonBean.HistoryBean bean = model.getData();
                            if (!TextUtils.isEmpty(bean.getTip())) {
                                tvTip.setText(bean.getTip());
                            }
                            if (bean.getList() != null && bean.getList().size() > 0) {
                                NovelEditHistoryAdapter adapter = new NovelEditHistoryAdapter(bean.getList());
                                LinearLayoutManager manager = new LinearLayoutManager(NovelEditHistoryActivity.this);
                                manager.setOrientation(LinearLayoutManager.VERTICAL);
                                recyclerView.setLayoutManager(manager);
                                recyclerView.setAdapter(adapter);
                                llNull.setVisibility(View.GONE);

                            }else{
                                llNull.setVisibility(View.VISIBLE);
                            }
                        }else{
                            llNull.setVisibility(View.VISIBLE);
                        }
                        dimsDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        llNull.setVisibility(View.VISIBLE);
                        dimsDialog();
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
